package com.ruoyi.gd.khbjkPlus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.khbjkPlus.domain.bo.ZKhbjkPlusBo;
import com.ruoyi.gd.khbjkPlus.domain.vo.ZKhbjkPlusVo;
import com.ruoyi.gd.khbjkPlus.domain.ZKhbjkPlus;
import com.ruoyi.gd.khbjkPlus.mapper.ZKhbjkPlusMapper;
import com.ruoyi.gd.khbjkPlus.service.IZKhbjkPlusService;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户报价库PlusService业务层处理
 *
 * @author ruoyi
 * @date 2024-07-19
 */
@RequiredArgsConstructor
@Service
public class ZKhbjkPlusServiceImpl implements IZKhbjkPlusService {

    private final ZKhbjkPlusMapper baseMapper;

    /**
     * 查询客户报价库Plus
     */
    @Override
    public ZKhbjkPlusVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户报价库Plus列表
     */
    @Override
    public TableDataInfo<ZKhbjkPlusVo> queryPageList(ZKhbjkPlusBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZKhbjkPlus> lqw = buildQueryWrapper(bo);
        Page<ZKhbjkPlusVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户报价库Plus列表
     */
    @Override
    public List<ZKhbjkPlusVo> queryList(ZKhbjkPlusBo bo) {
        LambdaQueryWrapper<ZKhbjkPlus> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZKhbjkPlus> buildQueryWrapper(ZKhbjkPlusBo bo) {
        StringUtils.trimEntityFields(bo);
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZKhbjkPlus> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getYw()), ZKhbjkPlus::getYw, bo.getYw());
        lqw.like(StringUtils.isNotBlank(bo.getLb()), ZKhbjkPlus::getLb, bo.getLb());
        lqw.like(StringUtils.isNotBlank(bo.getBm()), ZKhbjkPlus::getBm, bo.getBm());
        lqw.like(StringUtils.isNotBlank(bo.getXmlbbh()), ZKhbjkPlus::getXmlbbh, bo.getXmlbbh());
        lqw.like(StringUtils.isNotBlank(bo.getXmh()), ZKhbjkPlus::getXmh, bo.getXmh());
        lqw.like(StringUtils.isNotBlank(bo.getLb2()), ZKhbjkPlus::getLb2, bo.getLb2());
        lqw.like(StringUtils.isNotBlank(bo.getSfxm()), ZKhbjkPlus::getSfxm, bo.getSfxm());
        lqw.like(StringUtils.isNotBlank(bo.getZqd()), ZKhbjkPlus::getZqd, bo.getZqd());
        lqw.like(StringUtils.isNotBlank(bo.getDj()), ZKhbjkPlus::getDj, bo.getDj());
        lqw.like(StringUtils.isNotBlank(bo.getClfw()), ZKhbjkPlus::getClfw, bo.getClfw());
        lqw.like(StringUtils.isNotBlank(bo.getSfdw()), ZKhbjkPlus::getSfdw, bo.getSfdw());
        lqw.like(StringUtils.isNotBlank(bo.getSfbz()), ZKhbjkPlus::getSfbz, bo.getSfbz());
        lqw.like(StringUtils.isNotBlank(bo.getBz()), ZKhbjkPlus::getBz, bo.getBz());
        return lqw;
    }

    /**
     * 新增客户报价库Plus
     */
    @Override
    public Boolean insertByBo(ZKhbjkPlusBo bo) {
        ZKhbjkPlus add = BeanUtil.toBean(bo, ZKhbjkPlus.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户报价库Plus
     */
    @Override
    public Boolean updateByBo(ZKhbjkPlusBo bo) {
        ZKhbjkPlus update = BeanUtil.toBean(bo, ZKhbjkPlus.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZKhbjkPlus entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户报价库Plus
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int importData() {
        String excelUrl = "D:\\OA\\pc\\常规报价参考（以此为准）.xlsx";
        FileInputStream fileInputStream = null;
        int sum = 0;
        try {
            fileInputStream = new FileInputStream(excelUrl);
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            List<ZKhbjkPlusVo> list = new ArrayList<>();
            for (int i = 0; i < lastRowNum; i++) {
                if (i >= 2 && i < lastRowNum - 1) {
                    Row row = sheet.getRow(i);
                    ZKhbjkPlusVo zKhbjkPlusVo = new ZKhbjkPlusVo();
                    zKhbjkPlusVo.setYw(row.getCell(0) != null ? row.getCell(0).toString().trim() : "");  // 业务
                    zKhbjkPlusVo.setLb(row.getCell(1) != null ? row.getCell(1).toString().trim() : "");  // 类别
                    zKhbjkPlusVo.setBm(row.getCell(2) != null ? row.getCell(2).toString().trim() : "");  // 部门
                    zKhbjkPlusVo.setXmlbbh(row.getCell(3) != null ? row.getCell(3).toString().trim() : "");  // 项目类别编号
                    zKhbjkPlusVo.setXmh(row.getCell(4) != null ? row.getCell(4).toString().trim() : "");  // 项目号
                    zKhbjkPlusVo.setLb2(row.getCell(5) != null ? row.getCell(5).toString().trim() : "");  // 类别
                    zKhbjkPlusVo.setSfxm(row.getCell(6) != null ? row.getCell(6).toString().trim() : "");  // 收费项目
                    zKhbjkPlusVo.setZqd(row.getCell(7) != null ? row.getCell(7).toString().trim() : "");  // 准确度
                    zKhbjkPlusVo.setDj(row.getCell(8) != null ? row.getCell(8).toString().trim() : "");  // 等级
                    zKhbjkPlusVo.setClfw(row.getCell(9) != null ? row.getCell(9).toString().trim() : "");  // 测量范围
                    zKhbjkPlusVo.setSfdw(row.getCell(10) != null ? row.getCell(10).toString().trim() : "");  // 收费单位
                    zKhbjkPlusVo.setSfbz(row.getCell(11) != null ? row.getCell(11).toString().trim() : "");  // 收费标准
                    zKhbjkPlusVo.setBz(row.getCell(12) != null ? row.getCell(12).toString().trim() : "");  // 备注
                    list.add(zKhbjkPlusVo);
                }
            }
            // 批量添加数据
            sum = baseMapper.batchInsert(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sum;
    }
}
